package com.zeei.bfs.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.zeei.bfs.dao.DeptinfoDao;
import com.zeei.bfs.dao.RoleDao;
import com.zeei.bfs.dao.RoleMenuDao;
import com.zeei.bfs.dao.RolePntenterDao;
import com.zeei.bfs.dao.UserRoleDao;
import com.zeei.bfs.service.RoleService;
import com.zeei.bfs.vo.DeptinfoVO;
import com.zeei.bfs.vo.QueryRoleParamVO;
import com.zeei.bfs.vo.RoleVO;
import com.zeei.common.constant.GlobalConstant;
import com.zeei.common.utils.SessionInfoUtil;
import com.zeei.common.utils.TreeUtil;
import com.zeei.common.vo.TreeNodeVO;
import com.zeei.common.vo.UserVO;

@Service("roleService")
public class RoleServiceImpl extends BasicServiceImpl implements RoleService {

	@Autowired
	private RoleDao roleDao;

	@Autowired
	private RoleMenuDao roleMenuDao;

	@Autowired
	private UserRoleDao userRoleDao;

	@Autowired
	private RolePntenterDao rolePntenterDao;

	@Autowired
	private DeptinfoDao deptinfoDao;

	@Override
	public PageInfo<RoleVO> queryRole(QueryRoleParamVO queryRoleParam, UserVO user) {
		Map<String, Object> condition = queryRoleParam.convertConditionMap();
		// 获取用户所在当前部门以及旗下的所有部门的id
		List<String> deptIdList = getDeptIdList();
		condition.put("deptIdList", deptIdList);
		this.setPage(queryRoleParam.getPage(), queryRoleParam.getRows());
		List<RoleVO> list = roleDao.queryRoleByCondition(condition);
		return new PageInfo<RoleVO>(list);
	}

	private List<String> getDeptIdList() {
		UserVO user = SessionInfoUtil.getSessionUserInfo();
		String deptId = null;
		if (!GlobalConstant.ADMIN_NAME.equalsIgnoreCase(user.getLoginname())) {
			Integer id = user.getDeptid();
			deptId = id.toString();
			List<DeptinfoVO> list = deptinfoDao.queryDeptinfoByCondition(new HashMap<String, Object>());
			List<String> deptIds = getTreeNodeIds(list, deptId);
			return deptIds;
		}
		return new ArrayList<String>();
	}

	private List<String> getTreeNodeIds(List<DeptinfoVO> list, String deptId) {
		List<TreeNodeVO<DeptinfoVO>> treeList = TreeUtil.buildTreeNodeTree(list, deptId);
		List<String> deptIds = new ArrayList<String>();
		getTreeNodes(treeList, deptIds);
		return deptIds;
	}

	private void getTreeNodes(List<TreeNodeVO<DeptinfoVO>> treeList, List<String> deptIds) {
		for (TreeNodeVO<DeptinfoVO> node : treeList) {
			if (StringUtils.isNotEmpty(node.getId())) {
				deptIds.add(node.getId());
			}
			if (CollectionUtils.isNotEmpty(node.getChildren())) {
				getTreeNodes(node.getChildren(), deptIds);
			}
		}
	}

	@Override
	public boolean addRole(RoleVO role) {
		Integer count = roleDao.insertRole(role);
		return count > 0;
	}

	@Override
	public boolean editRole(RoleVO role) {
		if (role == null || role.getRoleId() == null) {
			return GlobalConstant.FAILED;
		}
		Integer count = roleDao.updateRoleById(role);
		return count > 0;
	}

	@Override
	public boolean dropRole(List<Integer> list) {
		if (CollectionUtils.isEmpty(list)) {
			return GlobalConstant.FAILED;
		}
		// 删除角色对应的关系,角色资源/角色菜单
		for (Integer roleId : list) {
			roleMenuDao.deleteRoleMenuByRoleId(roleId);
			userRoleDao.deleteUserRoleByRoleId(roleId);
			rolePntenterDao.deleteRolePntenterByRoleId(roleId);
		}
		// 删除角色
		Integer count = roleDao.deleteRoleByIds(list);
		return count > 0 ? GlobalConstant.SUCCEED : GlobalConstant.FAILED;
	}

	@Override
	public boolean validRoleName(RoleVO role) {
		if (role == null || StringUtils.isEmpty(role.getRoleName())) {
			return GlobalConstant.FAILED;// 失败
		}
		if (role.getRoleId() != null) {
			RoleVO queryVO = new RoleVO();
			queryVO.setRoleId(role.getRoleId());
			List<RoleVO> oldList = roleDao.queryRole(queryVO);
			if (CollectionUtils.isNotEmpty(oldList) && //
					role.getRoleName().equals(oldList.get(0).getRoleName())) {
				return GlobalConstant.SUCCEED;// 成功
			}
		}
		RoleVO queryVO = new RoleVO();
		queryVO.setRoleName(role.getRoleName());
		List<RoleVO> roleList = roleDao.queryRole(queryVO);
		if (CollectionUtils.isNotEmpty(roleList)) {
			return GlobalConstant.FAILED;// 失败
		}
		return GlobalConstant.SUCCEED;// 成功
	}

	@Override
	public List<RoleVO> queryAllRole(UserVO user, String userId) {
		List<String> deptIdList = getDeptIdList();
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("deptIdList", deptIdList);
		condition.put("userId", userId);
		List<RoleVO> roleList = roleDao.queryRoleByCondition(condition);
		return roleList;
	}

	/**
	 * 方法描述:获取部门的Id
	 *
	 * @return
	 */
	private static String getDeptId() {
		UserVO user = SessionInfoUtil.getSessionUserInfo();
		String deptId = null;
		if (!GlobalConstant.ADMIN_NAME.equalsIgnoreCase(user.getLoginname())) {
			Integer id = user.getDeptid();
			deptId = id.toString();
		}
		return deptId;
	}

}
